package com.frank.code_notes.C02_LinkedList;


import cn.hutool.core.lang.Console;
import com.frank.utils.ListNode;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @Description 203.移除链表元素
 * @Author zhoujr
 * @Data 2024/12/1 21:26
 */
public class L203RemoveLinkedListElements {

    public static ListNode removeElements(ListNode head, int val) {
        ListNode cur = head, prev = null, tmp = null;
        while (cur != null) {
            if (cur.val == val) {
                if (prev == null) {
                    head = head.next;
                } else {
                    prev.next = cur.next;
                }
            } else {
                prev = cur;
            }
            cur = cur.next;
        }
        return head;
    }

    public static void main(String[] args) {
        ListNode listNode = ListNode.toListNode(new ArrayList<>(Arrays.asList(1, 1, 2, 3, 4, 1, 5)));
        Console.log(removeElements(listNode, 1));
    }

}
